package cn.iocoder.yudao.module.jifenyuan.dal.mysql.wallet;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.wallet.WalletDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.jifenyuan.controller.app.wallet.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 记分员 钱包 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface WalletMapper extends BaseMapperX<WalletDO> {

    default PageResult<WalletDO> selectPage(AppWalletPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WalletDO>()
                .eqIfPresent(WalletDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WalletDO::getCoin, reqVO.getCoin())
                .eqIfPresent(WalletDO::getVip, reqVO.getVip())
                .betweenIfPresent(WalletDO::getVipExpireTime, reqVO.getVipExpireTime())
                .betweenIfPresent(WalletDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WalletDO::getId));
    }

    default List<WalletDO> selectList(AppWalletExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<WalletDO>()
                .eqIfPresent(WalletDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WalletDO::getCoin, reqVO.getCoin())
                .eqIfPresent(WalletDO::getVip, reqVO.getVip())
                .betweenIfPresent(WalletDO::getVipExpireTime, reqVO.getVipExpireTime())
                .betweenIfPresent(WalletDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WalletDO::getId));
    }

    default WalletDO selectWalletByUserId(Long userId) {
        return selectOne(WalletDO::getUserId,userId);
    }
    default  List<WalletDO> selectVipExpireList(){
     return    selectList(new LambdaQueryWrapperX<WalletDO>()

                .eqIfPresent(WalletDO::getVip, true)
                .ltIfPresent(WalletDO::getVipExpireTime, LocalDateTime.now()));

    }

    default int addWalletVip(Long userId,int day){
        WalletDO current = selectOne(WalletDO::getUserId, userId);
        WalletDO walletDO = new WalletDO();
        if(current.getVip()){

            LocalDateTime offset = LocalDateTimeUtil.offset(current.getVipExpireTime(), day, ChronoUnit.DAYS);
            walletDO.setVipExpireTime(offset);
        }else{
            walletDO.setVip(true);
            LocalDateTime offset = LocalDateTimeUtil.offset(LocalDateTimeUtil.now(), day, ChronoUnit.DAYS);
            walletDO.setVipExpireTime(offset);
        }

        return update(walletDO,new LambdaUpdateWrapper<WalletDO>().eq(WalletDO::getUserId,userId));
    }
    int addWalletCoin(@Param("userId") Long userId, @Param("coin")Long coin);
    int reduceWalletCoin(@Param("userId") Long userId, @Param("coin")Long coin);

}
